
package com.amado.course35Concurrent;

import java.util.concurrent.CountDownLatch;

/**
 * CountDownLatch 一个同步辅助类，在完成一组正在其他线程中执行的操作之前，它允许一个或多个线程一直等待。
 * 测试10个线程并发
 */
public class S01CountDownLatchTest {

	public static void main(String[] args) throws InterruptedException {
		
		CountDownLatch startSignal = new CountDownLatch(2);
		//相当于运动动陆续上场
		for (int i = 0; i < 20; i++) {
			Thread.sleep(100);
			new Thread(() -> {
				try {
					System.out.println(" 1 " + System.currentTimeMillis());
					//起跑线，全部等待
					startSignal.await();
					//接到信息，全部开始跑
					System.out.println(" 2 " + System.currentTimeMillis());
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}).start();
		}
		Thread.sleep(200);
		System.out.println("countdown");
		//发出信号，
		startSignal.countDown();
		Thread.sleep(200);
		System.out.println("countdown1");
		startSignal.countDown();
	}
}
